<template>
  <div class="page-container">
    <page_breadcrumb :getKeyMethod="init_table_model" @change="change_table_model" pageOption />
    <ex_super_table_page v-bind="option" ref="SuperTablePage" @selection-change="e => selection = e">
      <template v-slot:table-top>
        <el-alert :title="$tt('tip_returnGood_strategy_show')" type="warning" :closable="false" />
        <hr>
      </template>
    </ex_super_table_page>

    <ex_super_dialog_form ref="formDialog" v-bind="dialog" @afterSubmitSuccess="reload()">
    </ex_super_dialog_form>
  </div>
</template>
<script>
import FmsCurrencyChoose from '@/component/fms/fms-currency-choose.vue'
import superTablePageFunction from '@/mixin/super-table-page-function';
import emitOnFunction from '@/mixin/emit-on-function';

import page_table_model_manage from '@/mixin/page_table_model_manage';
let emitKey = 'returngood>strategy'
export default {
  title: 'label_return_order_strategy',
  mixins: [superTablePageFunction, emitOnFunction(emitKey), page_table_model_manage],
  components: { FmsCurrencyChoose },
  data() {
    return {
      status: "",
      selection: [],
      returnProcessMethodList:[],
      option: {
        search: {
          model: [
            { key: 'type', label: 'label_type', type: 'select', enum: 'OmsReturnOrderStrategyTypeEnum', },
            { key: 'isEffective', label: 'label_is_effective', type: 'select', enum: 'YesOrNoEnum', },

          ],
        },
        action: [
          {
            label: 'label_add_strategy', attrs: { icon: "el-icon-plus" }, click: () => {
              this.searchReturnProcessMethodList(() => {
                this['$refs']['formDialog'].showNew({
                  title: this.$tt('label_add_strategy'),
                  data: {},
                  onlySetData: true,
                })
              })

            }
          },
        ],

        table: {
          url: "/oms-api/client/OmsReturnOrderStrategyRpc/selectList.json",
          model: this.init_table_model(),
        },
      },
      dialog: {
        customClass: 'dialog_auto',
        detailUrl: '/oms-api/client/OmsReturnOrderStrategyRpc/detail.json',
        submitUrl: '/oms-api/client/OmsReturnOrderStrategyRpc/saveOrUpdate.json',

        form: {

          model: [
            { key: 'type', label: 'label_type', type: 'select', enum: 'OmsReturnOrderStrategyTypeEnum' },
            { key: 'isEffective', label: 'label_is_effective', type: 'select', enum: 'YesOrNoEnum' },

            { key: 'skuCurrencyCode', label: 'label_currency_kind', type: FmsCurrencyChoose },

            { key: 'minimumOrderTotalPrice', label: 'label_min_order_sum_value', },

            { key: 'maximumOrderTotalPrice', label: 'label_max_order_sum_value', },

            {
              key: 'returnHandlingMethod', label: 'label_sku_return_method', type: 'select',
              enum: 'SkuReturnMethodEnum',
              attrs: {
                listHandler: (list) => list.filter(it => this.returnProcessMethodList.includes(it))
              }
            },

          ],
          rules: {
            'type': { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
            'isEffective': { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
            'skuCurrencyCode': { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
            'minimumOrderTotalPrice': [
              { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
              {
                trigger: 'blur', validator: (r, v, cb) => {
                  if (Number(v) >= Number(this.$refs.formDialog.getFormData().maximumOrderTotalPrice)) {
                    cb(this.$tt('tip_no_more_equal_any', { type: this.$tt('label_max_order_sum_value') }))
                  }
                  cb()
                },
              }
            ],
            'maximumOrderTotalPrice': [
              { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
              {
                trigger: 'blur', validator: (r, v, cb) => {
                  if (Number(v) <= Number(this.$refs.formDialog.getFormData().minimumOrderTotalPrice)) {
                    cb(this.$tt('tip_no_less_equal_any', { type: this.$tt('label_min_order_sum_value') }))
                  }
                  cb()
                },
              }
            ],
            'returnHandlingMethod': { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
          },
        }
      },
    };
  },
  methods: {
    option_table_model() {
      return [
        { type: 'selection', clickToggle: true, keyGroup: 'label_table_operate' },
        {
          type: 'expandDesc', 'width': 100,
          expandDesc: {
            ...this.$defaultDescAttrs,
            model: [],
          },
        },

        { key: 'type', label: 'label_type', 'min-width': 120, 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$enumShow(v, "OmsReturnOrderStrategyTypeEnum") },
        { key: 'isEffective', label: 'label_is_effective', 'width': 120, 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$enumShow(v, "YesOrNoEnum") },

        {
          key: 'skuCurrencyCode', label: 'label_currency_kind', 'min-width': 120, 'show-overflow-tooltip': true,
        },
        {
          key: 'returnHandlingMethod', label: 'label_sku_return_method', 'width': 120, 'show-overflow-tooltip': true,
          formatter: (r, c, v, i) => this.$enumShow(v, "SkuReturnMethodEnum")
        },

        { key: 'minimumOrderTotalPrice', label: 'label_min_order_sum_value', 'width': 140, 'show-overflow-tooltip': true, },

        { key: 'maximumOrderTotalPrice', label: 'label_max_order_sum_value', 'width': 140, 'show-overflow-tooltip': true, },


        { key: 'gmtModify', label: 'label_modify_time', 'width': 140, 'show-overflow-tooltip': true, formatter: (r, c, v, i) => this.$dateFormat(v) },

        {
          type: 'operate', width: 210, operate: [
            {
              label: 'label_edit', click: (row) => {
                this.searchReturnProcessMethodList(() => {
                  this['$refs']['formDialog'].showNew({
                    title: this.$tt('label_edit_strategy'),
                    params: {
                      "id": row["id"]
                    },
                  })
                })

              }
            },
            {
              label: 'label_delete', type: "confirm-button", url: '/oms-api/client/OmsReturnOrderStrategyRpc/removeById.json', message: 'text_confirm_sure_delete',
              buttonClass: "button_text_danger", confirmButtonType: 'danger',
              params: (row) => { return { "id": row["id"] } }, done: ['reload', 'tip']
            },

          ]
        },
      ]
    },
    searchReturnProcessMethodList(callback) {
      if (this.$clickFast()) return
      if (_.isEmpty(this.returnProcessMethodList)) {
        this.$ajax({
          url: "/ums-api/client/CustomerCompanyClientRpc/detail4Client.json",
          loading: true,
          success: (res) => {
            if (res && res.data) {
              if (_.isEmpty(res.data.returnProcessMethodList)) {
                this.returnProcessMethodList = []
                // this.$alert(this.$tt('tip_client_customer_return_good_handle_way_no_set'), this.$tt("label_tip"), {
                //   type: 'warning'
                // }).catch(() => { });
                return
              } else {
                this.returnProcessMethodList = res.data.returnProcessMethodList
                callback(res.data.returnProcessMethodList)
              }
            } else {
              return this.$message.error(this.$tt('tip_customer_info_search_fail'))
            }
          },
          complete: () => {
          }
        })
      } else {
        callback(this.returnProcessMethodList)
      }
    },

  }
};

</script>
<style></style>
